#include "trayicon.h"
#include "QMessageBox"
trayicon::trayicon(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);

	initIcon();

	createActions();
	createTrayIcon();


	if (!connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
		this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))))
	{
		exit(0);
	}
	connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(messageClicked()));



	setIcon(0);
	trayIcon->show();
}

trayicon::~trayicon()
{

}

void trayicon::messageClicked()
{
	/*QMessageBox::information(0, tr("Systray"),
		tr("Sorry, I already gave what help I could.\n"
		"Maybe you should try asking a human?"));*/
}

void trayicon::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
	switch (reason) {
	case QSystemTrayIcon::Trigger:
		messageClicked();
		break;
	case QSystemTrayIcon::DoubleClick:
	//	iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1)
	//		% iconComboBox->count());
		break;
	case QSystemTrayIcon::MiddleClick:
		showMessage();
		break;
	default:
		;
	}
}

void trayicon::showMessage()
{
	/*QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(
		typeComboBox->itemData(typeComboBox->currentIndex()).toInt());
	trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon,
		durationSpinBox->value() * 1000);*/
}

void trayicon::createActions()
{
	minimizeAction = new QAction(tr("Mi&nimize"), this);
	connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));

	maximizeAction = new QAction(tr("Ma&ximize"), this);
	connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));

	restoreAction = new QAction(tr("&Restore"), this);
	connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));

	quitAction = new QAction(tr("&Quit"), this);
	connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}


void trayicon::createTrayIcon()
{
	trayIconMenu = new QMenu(this);
	trayIconMenu->addAction(minimizeAction);
	trayIconMenu->addAction(maximizeAction);
	trayIconMenu->addAction(restoreAction);
	trayIconMenu->addSeparator();
	trayIconMenu->addAction(quitAction);

	trayIcon = new QSystemTrayIcon(this);
	trayIcon->setContextMenu(trayIconMenu);
}

void trayicon::setIcon(int index)
{
	QIcon icon = _icon[index];
	trayIcon->setIcon(icon);
	setWindowIcon(icon);
	trayIcon->setToolTip(_toolTip[index]);
}